package artificialNeuralNetworks.Demo;

import java.util.ArrayList;
import java.util.LinkedHashSet;

import artificialNeuralNetworks.ANN.ANNDataExtractor;
import artificialNeuralNetworks.ANN.ANNExample;
import artificialNeuralNetworks.ANN.BackPropagation;
import artificialNeuralNetworks.ANN.NeuralNetwork;

/**
 * FileName: TestIdentity.java
 * 
 * @Description:
 * 
 * @author Xunhu(Tiger) Sun
 *         email: TigerSun86@gmail.com
 * @date Mar 6, 2014 10:12:37 PM
 */
public class TestIdentity {
    private static final String ATTR_FILE_URL = "http://cs.fit.edu/~pkc/classes/ml/data/iris-attr.txt";
    private static final String TRAIN_FILE_URL = "http://cs.fit.edu/~pkc/classes/ml/data/identity-train.txt";
    private static final String TEST_FILE_URL = "http://cs.fit.edu/~pkc/classes/ml/data/iris-test.txt";

    public static void main(final String[] args) {
        final LinkedHashSet<ANNExample> exSet = getExampleSet(TRAIN_FILE_URL);
        final IdentityProblem ip = new IdentityProblem();
        final NeuralNetwork net = BackPropagation
                .learn(exSet, 0.1, 0.1, 8, 8, 3);
        int count = 0;
        for (ANNExample ex : exSet) {
            ArrayList<Double> result = net.getV(ex.x);
            boolean correct = true;
            for (int i = 0; i < result.size(); i++) {
                double ret = result.get(i);
                double train = ex.t.get(i);
                if (Math.abs(ret - train) > 0.05) {
                    correct = false;
                }
            }
            System.out.println("train: " + ex.t + " result: " + result + " answer: "
                    + correct);
            if (correct) {
                count++;
            }
        }
        System.out.println("Correct ratio: " + count / 8.0);

    }

    private static LinkedHashSet<ANNExample> getExampleSet(final String examFName) {
        final LinkedHashSet<ANNExample> exSet = new LinkedHashSet<ANNExample>();
        final ANNDataExtractor in = new ANNDataExtractor(examFName);

        while (true) {
            final String line = in.nextLine();
            if (line == null) {
                break;
            }
            // 2 Space
            final String[] examStr = line.split("  ");
            final ANNExample ex = new ANNExample();
            for (String str : examStr[0].split(" ")) { // X.
                ex.x.add(Double.parseDouble(str));
            }
            for (String str : examStr[1].split(" ")) { // Train.
                //ex.t.add(Double.parseDouble(str));
                if (str.equals("1")){
                    ex.t.add(0.95);
                } else {
                    ex.t.add(0.05);
                }
            }
            exSet.add(ex);
        }

        in.close();

        if (exSet.isEmpty()) {
            System.err.println("No example in: " + examFName);
            return null;
        } else {
            return exSet;
        }
    }
}
